package com.nus.database;

import com.nus.beans.*;
import com.nus.database.util.DataReadUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA. User: dell Date: 9/21/12 Time: 12:03 AM To change
 * this template use File | Settings | File Templates.
 */
public class UserInfoDBManager extends DBManager {

    public UserInfoDBManager(Connection connection) {
        super(connection);
    }

    public List<Project> getUserProjects(int userId) {
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Project> projects = new ArrayList<Project>();
        Project project;
        try {
            statement = connection.prepareStatement(SQLStatements.GET_ASSIGNED_PROJECTS);
            statement.setInt(1, userId);
            resultSet = statement.executeQuery();

            while (resultSet.next()) {
                project = DataReadUtils.getProjectInformation(resultSet);
                projects.add(project);
            }
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        } finally {
            close(statement, resultSet);
        }
        return projects;
    }

    public List<UserEvaluation> getUserEvaluations(int userId) {
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<UserEvaluation> userEvaluationList = new ArrayList<UserEvaluation>();
        UserEvaluation userEvaluation;
        User user;
        try {
            statement = connection.prepareStatement(SQLStatements.GET_USER_EVALUATIONS);
            statement.setInt(1, userId);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                userEvaluation = DataReadUtils.getUserEvaluation(resultSet);
                userEvaluationList.add(userEvaluation);
            }
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        } finally {
            close(statement, resultSet);
        }
        return userEvaluationList;
    }

    public void addEvaluation(UserEvaluation userEvaluation) {
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(SQLStatements.CRAETE_USER_EVALUATION);
            statement.setInt(1, userEvaluation.getUserId());
            statement.setString(2, userEvaluation.getEvaluation());
            statement.setInt(3, userEvaluation.getEnteredBy().getUserId());
            statement.executeQuery();
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        } finally {
            close(statement);
        }
    }

    public UserEvaluation getUserEvaluation(int userEvaluationId) {
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        UserEvaluation userEvaluation = null;
        try {
            statement = connection.prepareStatement(SQLStatements.GET_USER_EVALUATION_BY_ID);
            statement.setInt(1, userEvaluationId);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                userEvaluation = DataReadUtils.getUserEvaluation(resultSet);
            }
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        } finally {
            close(statement, resultSet);
        }
        return userEvaluation;
    }

    public void updateUserEvaluation(UserEvaluation userEvaluation) {
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(SQLStatements.UPDATE_USER_EVALUATION);
            statement.setString(1, userEvaluation.getEvaluation());
            statement.setInt(2, userEvaluation.getId());
            statement.executeUpdate();
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        } finally {
            close(statement);
        }
    }

    public void deleteUserEvaluation(int userEvaluationId) {
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(SQLStatements.DELETE_USER_EVALUATION);
            statement.setInt(1, userEvaluationId);
            statement.executeUpdate();
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        } finally {
            close(statement);
        }
    }
}
